using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VPBED
    {
        public DataRow Convert2PBED(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryPBED(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["PBMANDANT"] = " ".PadRight(2 ); 
         dwReturn["PBBDZEI"] = " ".PadRight(12 ); 
         dwReturn["PBDATE"] = " ".PadRight(8 ); 
         dwReturn["PBVERSION"] = " ".PadRight(3 ); 
         dwReturn["PBKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["PBWERK"] = " ".PadRight(3 ); 
         dwReturn["PBABT"] = " ".PadRight(3 ); 
         dwReturn["PBWAKZ"] = " ".PadRight(1 ); 
         dwReturn["PBART"] = " ".PadRight(20 ); 
         dwReturn["PBVAR"] = " ".PadRight(3 ); 
         dwReturn["PBFARBE"] = " ".PadRight(10 ); 
         dwReturn["PBDESS"] = " ".PadRight(10 ); 
         dwReturn["PBCOLORIT"] = " ".PadRight(10 ); 
         dwReturn["PBBESCHICH"] = " ".PadRight(10 ); 
         dwReturn["PBZUSTAND"] = " ".PadRight(4 ); 
         dwReturn["PBAUFM"] = " ".PadRight(2 ); 
         dwReturn["PBVERP"] = " ".PadRight(4 ); 
         dwReturn["PBGROE"] = " ".PadRight(7 ); 
         dwReturn["PBLO"] = " ".PadRight(2 ); 
         dwReturn["PBPLNMG"] = " ".PadRight(0 ); 
         dwReturn["PBPLNMG2"] = " ".PadRight(0 ); 
         dwReturn["PBENTMG"] = " ".PadRight(0 ); 
         dwReturn["PBERLKZ"] = " ".PadRight(1 ); 
         dwReturn["PBPRIO"] = " ".PadRight(1 ); 
         dwReturn["PBKUNDNRB"] = " ".PadRight(8 ); 
         dwReturn["PBFILLER"] = " ".PadRight(130 ); 
         dwReturn["PBAENZL"] = " ".PadRight(0 ); 
         dwReturn["PBAENB"] = " ".PadRight(8 ); 
         dwReturn["PBAEND"] = " ".PadRight(0 ); 
         dwReturn["PBNEUB"] = " ".PadRight(8 ); 
         dwReturn["PBNEUD"] = " ".PadRight(0 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapPBED(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SavePBED(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryPBED(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapPBED(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertPBED(dwLocal, ITXDBVERSION);
            }
            UpdatePBED(dwLocal);
        }
        public void InsertPBED(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO PBED(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryPBED(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM PBED WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM PBED  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "PBED";
            Return = Result;
            return Return;
        }
        public void DeletePBED(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM PBED   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdatePBED(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE PBED SET "
                    +"PBMANDANT= :PBMANDANT,"
                    +"PBBDZEI= :PBBDZEI,"
                    +"PBDATE= :PBDATE,"
                    +"PBVERSION= :PBVERSION,"
                    +"PBKUNDNR= :PBKUNDNR,"
                    +"PBWERK= :PBWERK,"
                    +"PBABT= :PBABT,"
                    +"PBWAKZ= :PBWAKZ,"
                    +"PBART= :PBART,"
                    +"PBVAR= :PBVAR,"
                    +"PBFARBE= :PBFARBE,"
                    +"PBDESS= :PBDESS,"
                    +"PBCOLORIT= :PBCOLORIT,"
                    +"PBBESCHICH= :PBBESCHICH,"
                    +"PBZUSTAND= :PBZUSTAND,"
                    +"PBAUFM= :PBAUFM,"
                    +"PBVERP= :PBVERP,"
                    +"PBGROE= :PBGROE,"
                    +"PBLO= :PBLO,"
                    +"PBPLNMG= :PBPLNMG,"
                    +"PBPLNMG2= :PBPLNMG2,"
                    +"PBENTMG= :PBENTMG,"
                    +"PBERLKZ= :PBERLKZ,"
                    +"PBPRIO= :PBPRIO,"
                    +"PBKUNDNRB= :PBKUNDNRB,"
                    +"PBFILLER= :PBFILLER,"
                    +"PBAENZL= :PBAENZL,"
                    +"PBAENB= :PBAENB,"
                    +"PBAEND= :PBAEND,"
                    +"PBNEUB= :PBNEUB,"
                    +"PBNEUD= :PBNEUD,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("PBMANDANT", OracleType.NVarChar).Value = dwData["PBMANDANT"].ToString(); 
          or1.Parameters.Add("PBBDZEI", OracleType.NVarChar).Value = dwData["PBBDZEI"].ToString(); 
          or1.Parameters.Add("PBDATE", OracleType.NVarChar).Value = dwData["PBDATE"].ToString(); 
          or1.Parameters.Add("PBVERSION", OracleType.NVarChar).Value = dwData["PBVERSION"].ToString(); 
          or1.Parameters.Add("PBKUNDNR", OracleType.NVarChar).Value = dwData["PBKUNDNR"].ToString(); 
          or1.Parameters.Add("PBWERK", OracleType.NVarChar).Value = dwData["PBWERK"].ToString(); 
          or1.Parameters.Add("PBABT", OracleType.NVarChar).Value = dwData["PBABT"].ToString(); 
          or1.Parameters.Add("PBWAKZ", OracleType.NVarChar).Value = dwData["PBWAKZ"].ToString(); 
          or1.Parameters.Add("PBART", OracleType.NVarChar).Value = dwData["PBART"].ToString(); 
          or1.Parameters.Add("PBVAR", OracleType.NVarChar).Value = dwData["PBVAR"].ToString(); 
          or1.Parameters.Add("PBFARBE", OracleType.NVarChar).Value = dwData["PBFARBE"].ToString(); 
          or1.Parameters.Add("PBDESS", OracleType.NVarChar).Value = dwData["PBDESS"].ToString(); 
          or1.Parameters.Add("PBCOLORIT", OracleType.NVarChar).Value = dwData["PBCOLORIT"].ToString(); 
          or1.Parameters.Add("PBBESCHICH", OracleType.NVarChar).Value = dwData["PBBESCHICH"].ToString(); 
          or1.Parameters.Add("PBZUSTAND", OracleType.NVarChar).Value = dwData["PBZUSTAND"].ToString(); 
          or1.Parameters.Add("PBAUFM", OracleType.NVarChar).Value = dwData["PBAUFM"].ToString(); 
          or1.Parameters.Add("PBVERP", OracleType.NVarChar).Value = dwData["PBVERP"].ToString(); 
          or1.Parameters.Add("PBGROE", OracleType.NVarChar).Value = dwData["PBGROE"].ToString(); 
          or1.Parameters.Add("PBLO", OracleType.NVarChar).Value = dwData["PBLO"].ToString(); 
          or1.Parameters.Add("PBPLNMG", OracleType.NVarChar).Value = dwData["PBPLNMG"].ToString(); 
          or1.Parameters.Add("PBPLNMG2", OracleType.NVarChar).Value = dwData["PBPLNMG2"].ToString(); 
          or1.Parameters.Add("PBENTMG", OracleType.NVarChar).Value = dwData["PBENTMG"].ToString(); 
          or1.Parameters.Add("PBERLKZ", OracleType.NVarChar).Value = dwData["PBERLKZ"].ToString(); 
          or1.Parameters.Add("PBPRIO", OracleType.NVarChar).Value = dwData["PBPRIO"].ToString(); 
          or1.Parameters.Add("PBKUNDNRB", OracleType.NVarChar).Value = dwData["PBKUNDNRB"].ToString(); 
          or1.Parameters.Add("PBFILLER", OracleType.NVarChar).Value = dwData["PBFILLER"].ToString(); 
          or1.Parameters.Add("PBAENZL", OracleType.NVarChar).Value = dwData["PBAENZL"].ToString(); 
          or1.Parameters.Add("PBAENB", OracleType.NVarChar).Value = dwData["PBAENB"].ToString(); 
          or1.Parameters.Add("PBAEND", OracleType.NVarChar).Value = dwData["PBAEND"].ToString(); 
          or1.Parameters.Add("PBNEUB", OracleType.NVarChar).Value = dwData["PBNEUB"].ToString(); 
          or1.Parameters.Add("PBNEUD", OracleType.NVarChar).Value = dwData["PBNEUD"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
